<?php
class Thief extends CI_Controller
{
	public function __constructor()
	{
		parent::__constructor();
	}

	public function test($p =0){
		//从第一个开始
			
			$crons = $this->db->get_where('articlecron', array('status'=>0), 20, $p);
			$crons_res = $crons->result();
			
			if($crons_res){
				foreach($crons_res as $k=>$v){
					$id = $this->set_article($v->fromurl);
					
					$this->db->where('id', $v->id);
					if($id){
						$uparr =  array('status'=>1);
					}else{
						$uparr =  array('status'=>-1);
					}
					$this->db->update('articlecron',$uparr);
					$p = $v->id;
				}
				$this->load->helper('url');
				redirect('thief/test/'.$p, 'refresh');
			}else{ //没有找到cron
				die('May be Completed!!!');			
			}
		
	}

	

	//得到任务列表 
	public function get_articlecron(){
		$uri="http://www.56885.net/new_list.asp?id=35&page=";

		//通过解析分页链接，获得最后一页的page值
		$str = file_get_contents($uri.'1');
		$p = '/&page=([\d]+)">&gt;&gt;<\/a>/';

		preg_match($p, $str, $res);
		$the_last_page = intval($res[1]);

		//循环各个页面。
		for($i=1; $i<=$the_last_page; $i++){
			$this->get_links_frompage($uri.$i);
		}
	
	}
	public function get_links_frompage($uri){
		$baseurl = "http://www.56885.net/";
		
		$str = file_get_contents($uri);

		$p = '/<a href=([^\s]*) target="_blank" class=f12>([^<]*)<\/a>/';
		preg_match_all($p, $str, $res);
		//print_r($res);
		if($res){
			$arr_url=$res[1];
			$arr_title=$res[2];
			foreach($arr_url as $k=>$u){
				if($arr_title[$k]){
					$arr_title[$k] = iconv('GBK', 'UTF-8', $arr_title[$k]);
					$this->db->insert('articlecron', array(
						'fromurl'=>	$baseurl . $u,
						'title'	=> $arr_title[$k],
						'createtime'=> date("Y-m-d H:i:s")
					));
				}
			}
		}

	}

	public function set_article($uri){
		
		$p = '/<div id="ad02">.*?<\/div>/';
//		$uri='http://www.56885.net/news/2012620/325288.html';
//		$uri='http://www.56885.net/news/2010724/220025.html';
		$str = file_get_contents($uri);
		$str = $this->replace($p, '', $str);
		$p = '/<div class="newscontent">(.*?)<\/div><\/div><div class="news-right">/';
		$content = $this->get_content($p, $str, 1);

		$ph = '/<h1>([^<\/h1>]*)<\/h1>/';
		$title = $this->get_content($ph, $str, 1);

		//print_r($title);die();
		if($content && $title){
			$content = iconv('GBK','UTF-8', $content);
			$title = iconv('GBK', 'UTF-8', $title);
			$this->db->insert( 'article',
				array('title'=>$title, 
						'fromurl'=>$uri,
						'createtime'=>date("Y-m-d H:i:s"))	);
			$id =$this->db->insert_id();
			
			if($id){
				$this->db->insert( 'articlecontent',array('id'=>$id,'content'=>$content	)	);				
			}
			return $id;
		}else{
			//写入失败
			//echo ' writing error...url=', $uri;
			return 0;
		}
	}

	public function lists(){
		$r = $this->db->get('articlecontent');
		print_r($r->result());
	}



	/**
	*解析地址
	@return content page url links
	**/
	public function get_content_pagelinks(){
	}

	/*
	替换掉内容
	*/
	public function replace($pattern, $replacement, $str){
		$str = preg_replace($pattern, $replacement, $str);
		return $str;
	}

	/**
	得到相应位置的内容。由正则的括号位置决定
	**/
	public function get_content($pattern, $str, $indexofarray){
		preg_match($pattern, $str, $result);
		if(isset($result) && !empty($result) ){
			return $result[$indexofarray];
		}else{
			return NULL;
		}
	}



	public function index($i=0){
		//print_r($i);
		$uri='http://www.56885.net/news/2012620/325288.html';
		$this->getContent($uri);
	}

	//测试程序
	public function getContent($url){
		if(strpos($url, 'http')==0){
			$str = file_get_contents($url);
			//print_r($str);

//			$mat_title = '/<h1>([^<\/]*)<\/h1>/';

//			preg_match($mat_title, $str, $mytitle);
//			preg_match("/56/", $url, $mytitle);
//			echo $url;
//			print_r($mytitle);

			$mat_content = '/\'u849616\';<\/script><script src="http:\/\/cpro.baidu.com\/cpro\/ui\/c.js" type="text\/javascript"><\/script><\/div>([^<\/div><\/div><div class="news-right">]*)<\/div><\/div><div class="news-right">/';
//			$mat_content = '/<div class="mcHeader2">([^<\div>]*)<\/div>/';
			$mat_content = '/<span class="time">([^<\/span>]*)<\/span>/';
			$mat_content = '/<div class="newscontent">([^<\/div><\/div><div class="news-right">]*)<\/div><\/div><div class="news-right">/';
			$mat_content = '/<\/div>([^<\/div>]*)<\/div>/';
			$mat_content = '/<div id=\"ad02\">([^<\div>]*)<\/div>/';
			preg_match_all($mat_content, $str, $mycontent);
			print_r($mycontent);
		}else{
			echo 'not allowed url';
		}
	}
}